package sadi.client;


import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import sadi.client.ListItem;
import sadi.client.WikiItem;

import com.jidesoft.swing.*;

/*
 * This class is just for testing ContentPanel
 */
public class GUI extends JFrame
{
	/*****************************************************************
	 * INTERFACE RELATED VARIABLES
	 *****************************************************************/
	/*********Main JPanel Wrappers**********/
	private JPanel pnlHeader;
	private JPanel pnlLeft;
	private JPanel pnlCenter;
	
	/*********Sub-Panels**************/
	private JPanel pnlWelcome;
	private JPanel pnlUser;
	private JPanel pnlSearch;
	private JPanel pnlLinks;
	private JPanel pnlChat;
	private JPanel pnlChatButton;
	private JPanel pnlChatArea;
	private JPanel pnlHighlight;
	private JPanel pnlWiki;
	private JPanel pnlFunc;
	
	/*************JLabel**************/
	private JLabel jlbWelcomeMsg;
	private JLabel jlbUsername;
	private JLabel jlbTitle;
	private JLabel jlbDesc;
	
	/*************JButton*************/
	private JButton jbtLogin;
	private JButton jbtRegister;
	private JButton jbtLogout;
	private JButton jbtSearch;
	private JButton jbtAddWiki;
	private JButton jbtApproveWiki;
	private JButton jbtReportedWiki;
	private JButton jbtUpdateWelcome;
	private JButton jbtChat;
	private JButton jbtEdit;
	private JButton jbtReport;
	private JButton jbtDelete;
	private JButton jbtDone;
	private JButton jbtClear;
	private JButton jbtApprove;
	private JButton jbtApproveAll;
	private JButton jbtDeny;
	private JButton jbtDeleteAll;
	private JButton jbtIgnore;
	
	/*************JTextFields*************/
	private JTextField jtfSearch;
	private JTextField jtfChat;
	
	/*************JTextArea************/
	private JTextArea jtaChatHistory;
	
	/*************JScrollPane*************/
	private JScrollPane jsp = null;
	private SimpleScrollPane ssp = null;
	
	/*********Layout Manager Variables*********/
	private GridBagConstraints gbc;
	private GridLayout grid = new GridLayout(2, 1, 5, 0);//For text in WikiItems
	
	/*********Other Interface Related Variables************/
	private TitledBorder tb;
	private Dimension sizeHL = new Dimension(400,40);//Dimension for Highlight Items
	
	/*****************************************************************
	 * FUNCTIONAL RELATED VARIABLES
	 *****************************************************************/
	/**********Storage for welcome message***********/
	private String welcomeMsg;
	
	/*********Storages for Wikis************/
	private Vector <WikiItem> vtHighlights = new Vector<WikiItem>();
	private Vector <WikiItem> vtWikis = new Vector<WikiItem>();
 	private WikiItem wiki;
 	private Vector <ListItem> vtListItems;
 	private ListItem listItem;
 	private int currWikiID, currAuthor ;
 	private String currTitle, currDesc, currContent ;
 	
	
 	/*********Storages for User************/
 	private String username;
	private int userID;
	private boolean isLogin;
	private boolean isAdmin;
	
	private int UserLevel = ADMIN;
	private static final int ADMIN = 0;
	private static final int USER  = 1;
	private static final int GUEST = 2;
	
	private int ContentType = VIEW;
	private static final int LIST_VIEW = 0;
	private static final int VIEW = 1;
	private static final int EDIT = 2;
	private static final int NEW = 3;
	private static final int LIST_APPROVE = 4;
	private static final int APPROVE = 5;
	private static final int LIST_REPORTED = 6;
	private static final int REPORTED = 7;
	
	/*******Others useful variables*******/
	int x, y;
	
	/***********FOR DEVELOPERS USE ONLY*******************/
	ArrayList<ArrayList> array;
	
	/*****************************************************************
	 * MVC RELATED VARIABLES
	 *****************************************************************/
	/*
	 * private Model model;
	 * 
	 */
	private Controller controller;

	/*****************************************************************
	 * CONSTRUCTOR
	 *****************************************************************/
	public GUI()
	{
		//-----------------Initialises the MVC Components------------------
		/*
		 * this.model = model;
		 * this.controller = controller;
		 */
		//controller = new Controller(/*model, */this);
		
		//-----------------Set the Layout Manager for JFrame----------------
		this.setLayout(new GridBagLayout());
		gbc = new GridBagConstraints();
		setUpGBC();
		
		
		//-------------initialises all the Functional and GUI components----------------
		/*****************************************************************
		 * FUNCTIONAL RELATED VARIABLES
		 *****************************************************************/
		/*********Storages for Wikis************/
		vtHighlights = new Vector<WikiItem>();
		vtWikis = new Vector<WikiItem>();
	 	vtListItems = new Vector<ListItem> ();
	    welcomeMsg /*Get the welcome msg at startup*/ = "";
		
	 	/*********Storages for User************/
		isLogin = false;
		isAdmin = false;
		UserLevel = GUEST;
		ContentType = LIST_VIEW;

		username = "Stanley";//testing
		userID = 123;//testing
		currAuthor = 123;//testing
		
		/*****************************************************************
		 * INTERFACE RELATED VARIABLES
		 *****************************************************************/
		/*********Main JPanel Wrappers**********/
		pnlHeader = new JPanel(new BorderLayout());
		pnlLeft   = new JPanel(new GridBagLayout());
		pnlCenter = new JPanel(new BorderLayout());
		
		/*********Sub-Panels**************/
		pnlWelcome      = new JPanel();
		pnlUser         = new JPanel();
		pnlSearch       = new JPanel(new GridBagLayout());
		pnlLinks        = new JPanel(new GridBagLayout());
		pnlChat         = new JPanel(new BorderLayout());
		pnlChatButton   = new JPanel(new BorderLayout());
		pnlChatArea     = new JPanel(new BorderLayout());
		pnlHighlight    = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
		pnlWiki         = new JPanel(new GridBagLayout());
		pnlFunc         = new JPanel(new FlowLayout(FlowLayout.LEFT, 5 , 0));
		
		/*************JLabel**************/
		jlbWelcomeMsg = new JLabel(welcomeMsg);
		jlbUsername   = new JLabel(username);
		jlbTitle = new JLabel("Title: ");
		jlbDesc = new JLabel("Description: ");
		
		/*************JButton*************/
		jbtRegister          = new JButton("Register");
		jbtLogin             = new JButton("Login");
		jbtLogout            = new JButton("Logout");
		jbtSearch            = new JButton("                    Search                    ");
		jbtAddWiki           = new JButton("Add a Wiki");
		jbtApproveWiki       = new JButton("Approve Wiki");
		jbtReportedWiki      = new JButton("Reported Wiki");
		jbtUpdateWelcome     = new JButton("Update Welcome Message");
		jbtChat              = new JButton("Send");
		jbtEdit              = new JButton("Edit");
		jbtReport            = new JButton("Report Abuse");
		jbtDelete            = new JButton("Delete");
		jbtDone              = new JButton("Done");
		jbtClear             = new JButton("Clear");
		jbtApprove           = new JButton("Approve");
		jbtApproveAll        = new JButton("Approve All");
		jbtDeny              = new JButton("Deny");
		jbtDeleteAll         = new JButton("Delete All");
		jbtIgnore            = new JButton("Ignore");

		/*************JTextFields*************/
		jtfSearch = new JTextField();
		jtfChat   = new JTextField();
		
		/*********JScrollPane*********/
		//jsp = new JScrollPane(new JPanel());
		
		/*************JTextArea************/
		jtaChatHistory = new JTextArea();
		
		/*********Layout Manager Variables*********/
		gbc = new GridBagConstraints();
		
		//----------------- 2. layout all the GUI components------------------------

		/*****************************************************************
		 * Construct the pnlHeader
		 *****************************************************************/
		this.setWelcomeMsg("Welcome to Shared Wiki!");
		this.setUserPanel(isLogin);
		this.constructHeader();
		
		
		/*****************************************************************
		 * Construct the pnlLeft
		 *****************************************************************/
		//pnlSearch - appears for every kind of user
		this.addCompWeight(pnlSearch, jtfSearch, gbc, GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 1.0, 0.0);
		this.addCompWeight(pnlSearch, jbtSearch, gbc, GridBagConstraints.HORIZONTAL, 0, 1, 1, 1, 1.0, 0.0);
		
		//pnlLinks
		setQuickLinks(isLogin, isAdmin, UserLevel);
		
		//pnlChat
		setChatPanel(isLogin);
		
		/*****************************************************************
		 * Construct the pnlCenter
		 *****************************************************************/
		//pnlHighlight
		setHighlights(/*ArrayList array*/);
		
		//setting up the SimpleScrollPane for pnlHighlight
		ssp = new SimpleScrollPane(pnlHighlight);
		ssp.setHorizontalUnitIncrement(35);
		ssp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		ssp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		//pnlWiki
		this.setModeListView();
		//this.setModeApprove();
		//this.setModeEdit();
		//this.setModeListApprove();
		//this.setModeListReported();
		//this.setModeNew();
		//this.setModeReported();
		//this.setModeView();
		
		
		/*****************************************************************
		 * Construct the main panels
		 *****************************************************************/
		if (UserLevel == ADMIN || UserLevel == USER)
		{
			//Create TitledBorders for all the sub-panels
			createTitledPanels(tb, "Search", pnlSearch, TitledBorder.CENTER ,TitledBorder.DEFAULT_POSITION, BorderFactory.createLineBorder(Color.black) );
			createTitledPanels(tb, "Quick Links", pnlLinks, TitledBorder.CENTER ,TitledBorder.DEFAULT_POSITION, BorderFactory.createLineBorder(Color.black));
			createTitledPanels(tb, "Live! Chat", pnlChat, TitledBorder.CENTER ,TitledBorder.DEFAULT_POSITION, BorderFactory.createLineBorder(Color.black));
			createTitledPanels(tb, "Today's Highlights", pnlHighlight, TitledBorder.LEFT ,TitledBorder.DEFAULT_POSITION, BorderFactory.createEmptyBorder());
		}
		
		//add all the sub-panels to pnlLeft
		this.addCompFill(pnlLeft, pnlSearch, gbc,GridBagConstraints.HORIZONTAL, 0, 0, 1, 1);
		this.addCompFill(pnlLeft, pnlLinks, gbc,GridBagConstraints.HORIZONTAL, 0, 1, 1, 1);
		if (UserLevel == ADMIN || UserLevel == USER)
			this.addCompWeight(pnlLeft, pnlChat, gbc,GridBagConstraints.BOTH, 0, 2, 1, 1, 1.0, 1.0);
		else//If User is not login, just add an empty JPanel
			this.addCompWeight(pnlLeft, new JPanel(), gbc,GridBagConstraints.BOTH, 0, 2, 1, 1, 1.0, 1.0);
		
		//add all the sub-panels to pnlCenter
		pnlCenter.add(ssp,BorderLayout.NORTH);
		pnlCenter.add(pnlWiki, BorderLayout.CENTER);
		pnlCenter.add(pnlFunc, BorderLayout.SOUTH);
		
		//Add the JPanels into JFrame using GridBagLayout
		this.addCompFill(this, pnlHeader, gbc, GridBagConstraints.BOTH, 0, 0, 2, 1);
		this.addCompFill(this, pnlLeft, gbc, GridBagConstraints.VERTICAL, 0, 1, 1, 1);
		this.addCompWeight(this, pnlCenter, gbc, GridBagConstraints.BOTH, 1, 1, 1, 1, 0.6, 0.9);

		/*****************************************************************
		 * Define the JFrame properties
		 *****************************************************************/
		this.setTitle("Shared Wiki");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1024,600);
		this.setResizable(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
		controller = new Controller(/*model, */this);
	}
	
	/*****************************************************************
	 * 3. methods to add the listeners to JButton
	 *****************************************************************/
	
	/*****************************************************************
	 * 4. methods to refresh the panels
	 *****************************************************************/
	
	/****************************************************************************
	 * THE REFRESHERS METHODS
	 ****************************************************************************/
	/*
	 * Set the Welcome Message
	 */
	public void setWelcomeMsg(String msg)
	{
		pnlWelcome.removeAll();
		jlbWelcomeMsg = new JLabel(msg);
		pnlWelcome.add(jlbWelcomeMsg);
		pnlWelcome.revalidate();
		pnlWelcome.repaint();
	}
	
	/*
	 * Refreshes pnlUser if User login/logout
	 */
	public void setUserPanel(boolean isLogin)
	{
		pnlUser.setAlignmentX(RIGHT_ALIGNMENT);
		if(isLogin)
		{
			pnlUser.add(jlbUsername);
			pnlUser.add(jbtLogout);
		}
		else
		{
			pnlUser.add(jbtRegister);
			pnlUser.add(jbtLogin);
		}
		
		pnlUser.revalidate();
		pnlUser.repaint();
	}
	/*
	 * Refreshes pnlQuickLink if User login/logout
	 */
	public void setQuickLinks(boolean isLogin, boolean isAdmin, int UserLevel)
	{
		x = 0; y = 0;
		
		if (isLogin && isAdmin && UserLevel == ADMIN)
		{
			this.addCompWeight(pnlLinks, jbtAddWiki, gbc,GridBagConstraints.HORIZONTAL, x, y++, 1, 1, 1.0, 0.9);
			this.addCompWeight(pnlLinks, jbtApproveWiki, gbc,GridBagConstraints.HORIZONTAL, x, y++, 1, 1, 1.0, 0.9);
			this.addCompWeight(pnlLinks, jbtReportedWiki, gbc,GridBagConstraints.HORIZONTAL, x, y++, 1, 1, 1.0, 0.9);
			this.addCompWeight(pnlLinks, jbtUpdateWelcome, gbc,GridBagConstraints.HORIZONTAL, x, y++, 1, 1, 1.0, 0.9);
		}
		
		else if (isLogin && !isAdmin && UserLevel == USER)
		{
			this.addCompWeight(pnlLinks, jbtAddWiki, gbc,GridBagConstraints.HORIZONTAL, x, y++, 1, 1, 1.0, 0.0);
		}
		else if (!isLogin && !isAdmin && UserLevel == GUEST)
		{
			//GUEST cannot do anything with the links
		}
		
		pnlLinks.revalidate();
		pnlLinks.repaint();
	}
	
	/*
	 * Refreshes pnlChat if User login/logout
	 */
	public void setChatPanel(boolean isLogin)
	{
		if(isLogin)
		{
			jtaChatHistory.setEditable(false);
			JScrollPane jspChatHistory = new JScrollPane(jtaChatHistory); 
			jspChatHistory.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jspChatHistory.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			pnlChatButton.add(jbtChat, BorderLayout.EAST);
			
			pnlChatArea.add(jtfChat,BorderLayout.CENTER);
			pnlChatArea.add(pnlChatButton, BorderLayout.SOUTH);
			
			pnlChat.add(jspChatHistory, BorderLayout.CENTER);
			pnlChat.add(pnlChatArea,BorderLayout.SOUTH);
		}
		else if(!isLogin)
		{
			//GUEST cannot do anything with the chat room
		}
		
		pnlChat.revalidate();
		pnlChat.repaint();
	}
	/*
	 * Refreshes the Highlights
	 */
	public void setHighlights(/*ArrayList array*/)
	{
		//Today's Highlights will be available everytime
		//Get information from database (ArrayList)
		array = this.createArrayList();//testing purposes
		pnlHighlight.removeAll();
		
		ListIterator li1 = array.listIterator();
		while (li1.hasNext())
		{
			ArrayList <String> str = (ArrayList<String>) li1.next();
			ListIterator li2 = str.listIterator();
			while(li2.hasNext())
			{
				int index = Integer.parseInt(li2.next().toString());
				this.createHighlightItem(index, li2.next().toString(), li2.next().toString());
				li2.next();//testing
			}
		}
		//display the items into the panel
		this.displayHighlights(vtHighlights, pnlHighlight);
		
		pnlHighlight.revalidate();
		pnlHighlight.repaint();
	}
	
	/*
	 * Refreshes the pnlWiki for different ContentType
	 */
	//0 = LIST_VIEW
	public void setModeListView(/*, ArrayList <ArrayList>array*/)
	{
		
		/**********Content Viewer**********/
		//Get information from database (ArrayList)
		array = this.createArrayList();//testing purposes
	
		ListIterator li1 = array.listIterator();
		while (li1.hasNext())
		{
			ArrayList <String> str = (ArrayList<String>) li1.next();
			ListIterator li2 = str.listIterator();
			while(li2.hasNext())
			{
				int index = Integer.parseInt(li2.next().toString());
				this.createWikiItem(index, li2.next().toString(), li2.next().toString());
				li2.next();//testing to throw out the author id
			}
		}
		
		this.displayWikiItems(vtWikis, pnlWiki);
		
		//setting up the JScrollPane for pnlWiki
		jsp = new JScrollPane(pnlWiki);
		jsp.setWheelScrollingEnabled(true);
		jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		pnlWiki.revalidate();
		pnlWiki.repaint();
		
		ContentType = LIST_VIEW;
	}
	
	// 1 = VIEW
	public void setModeView(/*ArrayList<String> array*/)
	{
		/**********Content Viewer**********/
		//Get information from database (ArrayList)
		array = this.createArrayList();//testing purposes
		ArrayList <String>str = array.get(0);//testing purposes
		
		ListIterator li = str.listIterator();
		currWikiID = Integer.parseInt(li.next().toString());
		currTitle = li.next().toString();
		currDesc = li.next().toString();
		//currContent = li.next().toString();
		currContent = "blablabla\nYO!";//testing.. Delete this line after the array is available
		currAuthor = Integer.parseInt(li.next().toString());
		
		JLabel jlbViewTitle = new JLabel(currTitle);
		JLabel jlbViewDesc = new JLabel(currDesc);
		JTextArea jtaViewContent = new JTextArea();
			jtaViewContent.setEditable(false);
			jtaViewContent.setText(currContent);
		jsp = new JScrollPane(jtaViewContent);
			jsp.setWheelScrollingEnabled(true);
			jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		pnlWiki.removeAll();
		//Add components into panel
		this.addCompWeight(pnlWiki, jlbTitle, gbc,GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 0.0, 0.0);
		this.addCompWeight(pnlWiki, jlbDesc, gbc, GridBagConstraints.HORIZONTAL, 0, 1, 1, 1, 0.0, 0.0);
		this.addCompWeight(pnlWiki, jlbViewTitle, gbc, GridBagConstraints.HORIZONTAL, 1, 0, 1, 1, 1.0, 0.0);
		this.addCompWeight(pnlWiki, jlbViewDesc, gbc, GridBagConstraints.HORIZONTAL, 1, 1, 1, 1, 1.0, 0.0);
		this.addCompWeight(pnlWiki, jsp, gbc, GridBagConstraints.BOTH, 0, 2, 2, 1, 1.0, 1.0);
		
		/**********Functional Buttons**********/
		if(isLogin)
		{
			pnlFunc.add(jbtEdit);
			pnlFunc.add(jbtReport);
			pnlFunc.add(jbtDelete);
			if (!(userID == currAuthor))
			{
				jbtEdit.setEnabled(false);
				jbtDelete.setEnabled(false);
			}
		}
		
		pnlWiki.revalidate();
		pnlWiki.repaint();
		pnlFunc.revalidate();
		pnlFunc.repaint();
		
		ContentType = VIEW;
	}
	
	//2=EDIT
	public void setModeEdit()
	{
		if (isLogin && userID == currAuthor )
		{
			/**********Content Editor**********/
			JTextField jtfTitle = new JTextField();
				jtfTitle.setText(currTitle);
			JTextField jtfDesc = new JTextField();
				jtfDesc.setText(currDesc);
			JTextArea jtaEditContent = new JTextArea();
				jtaEditContent.setText(currContent);
			jsp = new JScrollPane(jtaEditContent);
				jsp.setWheelScrollingEnabled(true);
				jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			//Add components into panel
			this.addCompWeight(pnlWiki, jlbTitle, gbc,GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jlbDesc, gbc, GridBagConstraints.HORIZONTAL, 0, 1, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jtfTitle, gbc, GridBagConstraints.HORIZONTAL, 1, 0, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jtfDesc, gbc, GridBagConstraints.HORIZONTAL, 1, 1, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jsp, gbc, GridBagConstraints.BOTH, 0, 2, 2, 1, 1.0, 1.0);
			
			/**********Functional Buttons**********/
			pnlFunc.add(jbtDone);
			pnlFunc.add(jbtClear);
			
			pnlWiki.revalidate();
			pnlWiki.repaint();
			pnlFunc.revalidate();
			pnlFunc.repaint();
			
			ContentType = EDIT;
		}
	}
	
	//3=NEW
	public void setModeNew()
	{
		if(isLogin)
		{
			/**********Content Editor**********/
			JTextField jtfNewTitle = new JTextField();
			JTextField jtfNewDesc = new JTextField();
			JTextArea jtaNewContent = new JTextArea();
			JScrollPane jspNewContent = new JScrollPane(jtaNewContent);
				jspNewContent.setWheelScrollingEnabled(true);
				jspNewContent.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				jspNewContent.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

			this.addCompWeight(pnlWiki, jlbTitle, gbc,GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jlbDesc, gbc, GridBagConstraints.HORIZONTAL, 0, 1, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jtfNewTitle, gbc, GridBagConstraints.HORIZONTAL, 1, 0, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jtfNewDesc, gbc, GridBagConstraints.HORIZONTAL, 1, 1, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jspNewContent, gbc, GridBagConstraints.BOTH, 0, 2, 2, 1, 1.0, 1.0);
			
			/**********Functional Buttons**********/
			pnlFunc.add(jbtDone);
			pnlFunc.add(jbtClear);
			
			pnlWiki.revalidate();
			pnlWiki.repaint();
			pnlFunc.revalidate();
			pnlFunc.repaint();
			
			ContentType = NEW;
		}
	}
	
	//4=LIST_APPROVE
	public void setModeListApprove(/*ArrayList array*/)
	{
		if (isLogin && isAdmin && UserLevel == ADMIN)
		{
			vtListItems = new Vector<ListItem>();
			
			//Get information from database (ArrayList)
			array = this.createArrayList();//testing purposes
		
			ListIterator li1 = array.listIterator();
			while (li1.hasNext())
			{
				ArrayList <String> str = (ArrayList<String>) li1.next();
				ListIterator li2 = str.listIterator();
				while(li2.hasNext())
				{
					int index = Integer.parseInt(li2.next().toString());
					String title = li2.next().toString();
					li2.next();
					this.createListItem(index, title, li2.next().toString(), vtListItems);
				}
			}
			
			this.displayListItems("Wikis Waiting for Approval", "Author", "Select", vtListItems, pnlWiki);
			
			//setting up the JScrollPane for pnlWiki
			jsp = new JScrollPane(pnlWiki);
			jsp.setWheelScrollingEnabled(true);
			jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			
			/**********Functional Buttons**********/
			pnlFunc.add(jbtApprove);
			pnlFunc.add(jbtApproveAll);
			
			pnlWiki.revalidate();
			pnlWiki.repaint();
			pnlFunc.revalidate();
			pnlFunc.repaint();
			
			ContentType = LIST_APPROVE;
		}
	}
	//5=APPROVE
	public void setModeApprove(/*ArrayList array*/)
	{
		if (isLogin && isAdmin && UserLevel == ADMIN)
		{
			/**********Content Viewer**********/
			//Get information from database (ArrayList)
			array = this.createArrayList();//testing purposes
			ArrayList <String>str = array.get(0);//testing purposes
			
			ListIterator li = str.listIterator();
			currWikiID = Integer.parseInt(li.next().toString());
			currTitle = li.next().toString();
			currDesc = li.next().toString();
			//currContent = li.next().toString();
			currContent = "blablabla\nYO!";//testing.. Delete this line after the array is available
			currAuthor = Integer.parseInt(li.next().toString());
			
			JLabel jlbViewTitle = new JLabel(currTitle);
			JLabel jlbViewDesc = new JLabel(currDesc);
			JTextArea jtaViewContent = new JTextArea();
				jtaViewContent.setEditable(false);
				jtaViewContent.setText(currContent);
			jsp = new JScrollPane(jtaViewContent);
				jsp.setWheelScrollingEnabled(true);
				jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				
			//Add components into panel
			this.addCompWeight(pnlWiki, jlbTitle, gbc,GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jlbDesc, gbc, GridBagConstraints.HORIZONTAL, 0, 1, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jlbViewTitle, gbc, GridBagConstraints.HORIZONTAL, 1, 0, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jlbViewDesc, gbc, GridBagConstraints.HORIZONTAL, 1, 1, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jsp, gbc, GridBagConstraints.BOTH, 0, 2, 2, 1, 1.0, 1.0);
			
			pnlFunc.add(jbtApprove);
			pnlFunc.add(jbtDeny);
		}
		
		pnlWiki.revalidate();
		pnlWiki.repaint();
		pnlFunc.revalidate();
		pnlFunc.repaint();
		
		ContentType = APPROVE;
			
	}
	
	//6=LIST_REPORTED
	public void setModeListReported(/*ArrayList array*/)
	{
		if (isLogin && isAdmin && UserLevel == ADMIN)
		{
			vtListItems = new Vector<ListItem>();
			
			//Get information from database (ArrayList)
			array = this.createArrayList();//testing purposes
		
			ListIterator li1 = array.listIterator();
			while (li1.hasNext())
			{
				ArrayList <String> str = (ArrayList<String>) li1.next();
				ListIterator li2 = str.listIterator();
				while(li2.hasNext())
				{
					int index = Integer.parseInt(li2.next().toString());
					String title = li2.next().toString();
					li2.next();
					this.createListItem(index, title, li2.next().toString(), vtListItems);
				}
			}
			
			this.displayListItems("Reported Wikis", "Author", "Select", vtListItems, pnlWiki);
			
			//setting up the JScrollPane for pnlWiki
			jsp = new JScrollPane(pnlWiki);
			jsp.setWheelScrollingEnabled(true);
			jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			
			/**********Functional Buttons**********/
			pnlFunc.add(jbtDelete);
			pnlFunc.add(jbtDeleteAll);
			
			pnlWiki.revalidate();
			pnlWiki.repaint();
			pnlFunc.revalidate();
			pnlFunc.repaint();
			
			ContentType = LIST_REPORTED;
		}
	}
	
	
	//7=REPORTED
	public void setModeReported(/*ArrayList array*/)
	{
		if (isLogin && isAdmin && UserLevel == ADMIN)
		{
			/**********Content Viewer**********/
			//Get information from database (ArrayList)
			array = this.createArrayList();//testing purposes
			ArrayList <String>str = array.get(0);//testing purposes
			
			ListIterator li = str.listIterator();
			currWikiID = Integer.parseInt(li.next().toString());
			currTitle = li.next().toString();
			currDesc = li.next().toString();
			//currContent = li.next().toString();
			currContent = "blablabla\nYO!";//testing.. Delete this line after the array is available
			currAuthor = Integer.parseInt(li.next().toString());
			
			JLabel jlbViewTitle = new JLabel(currTitle);
			JLabel jlbViewDesc = new JLabel(currDesc);
			JTextArea jtaViewContent = new JTextArea();
				jtaViewContent.setEditable(false);
				jtaViewContent.setText(currContent);
			jsp = new JScrollPane(jtaViewContent);
				jsp.setWheelScrollingEnabled(true);
				jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
				jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				
			//Add components into panel
			this.addCompWeight(pnlWiki, jlbTitle, gbc,GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jlbDesc, gbc, GridBagConstraints.HORIZONTAL, 0, 1, 1, 1, 0.0, 0.0);
			this.addCompWeight(pnlWiki, jlbViewTitle, gbc, GridBagConstraints.HORIZONTAL, 1, 0, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jlbViewDesc, gbc, GridBagConstraints.HORIZONTAL, 1, 1, 1, 1, 1.0, 0.0);
			this.addCompWeight(pnlWiki, jsp, gbc, GridBagConstraints.BOTH, 0, 2, 2, 1, 1.0, 1.0);
			
			pnlFunc.add(jbtDelete);
			pnlFunc.add(jbtIgnore);
		}
		
		pnlWiki.revalidate();
		pnlWiki.repaint();
		pnlFunc.revalidate();
		pnlFunc.repaint();
		
		ContentType = REPORTED;
			
	}
	/****************************************************************************
	 * SUB-CONSTRUCTOR METHODS
	 ****************************************************************************/
	/*
	 * Construct the header
	 */
	
	public void constructHeader()
	{
		//add the sub-panels to the main panel
		pnlHeader.add(pnlWelcome, BorderLayout.CENTER);
		pnlHeader.add(pnlUser,BorderLayout.EAST);
	}
	
		
	/*
	 * Today's Highlight Related methods
	 */
	private void createHighlightItem(int wikiID, String title, String description)
	{
		wiki = new WikiItem(wikiID);
		JLabel lblTitle = new JLabel(title);
		JLabel lblDesc = new JLabel(description);
		
		wiki.setPreferredSize(sizeHL);
		wiki.setLayout(grid);
		wiki.add(lblTitle);
		wiki.add(lblDesc);
		
		vtHighlights.add(wiki);
	}
	
	private void displayHighlights(Vector <WikiItem> vector, JPanel pnl)
	{
		ListIterator<WikiItem> li = vector.listIterator();
		
		while (li.hasNext())
		{
			pnl.add(li.next());
		}
		
		pnl.revalidate();
		pnl.repaint();
	}
	
	/*
	 * Wiki's List Related methods
	 */
	private void createWikiItem(int wikiID, String title, String description)
	{
		wiki = new WikiItem(wikiID);
		JLabel lblTitle = new JLabel(title);
		JLabel lblDesc = new JLabel(description);
		wiki.setLayout(grid);
		wiki.add(lblTitle);
		wiki.add(lblDesc);
		
		vtWikis.add(wiki);
	}
	
	public void displayWikiItems(Vector <WikiItem> vector, JPanel pnl)
	{
		ListIterator<WikiItem> li = vector.listIterator();
		int y = 0;
		while (li.hasNext())
		{
			WikiItem wi = li.next();
			this.addCompWeight(pnl, wi, gbc, GridBagConstraints.BOTH, 0, y++, 1, 1, 1.0, 0.0);
		}
		
		JPanel dummyPanel = new JPanel();
		this.addCompWeight(pnl, dummyPanel, gbc, GridBagConstraints.BOTH, 0, y, 1, 1, 1.0, 1.0);
		
		pnl.revalidate();
		pnl.repaint();
	}
	
	/*
	 * List Items related method
	 */
	private void createListItem(int wikiID, String title, String author, Vector<ListItem> vector)
	{
		listItem = new ListItem(wikiID, title, author);
		vector.add(listItem);
	}
	
	public void displayListItems(String header1, String header2, String header3, Vector <ListItem> vector, JPanel pnl)
	{
		JLabel jlbWiki = new JLabel (header1);
		JLabel jlbAuthor = new JLabel (header2);
		JLabel jlbSelect = new JLabel (header3);
		
		//Print the header
		this.addCompWeight(pnl, jlbWiki, gbc, GridBagConstraints.HORIZONTAL, 0, 0, 1, 1, 0.6, 0.0);
		this.addCompWeight(pnl, jlbAuthor, gbc, GridBagConstraints.HORIZONTAL, 1, 0, 1, 1, 0.3, 0.0);
		this.addCompWeight(pnl, jlbSelect, gbc, GridBagConstraints.HORIZONTAL, 2, 0, 1, 1, 0.1, 0.0);
		
		//print the contents
		ListIterator<ListItem> li = vector.listIterator();
		int y = 1;
		while (li.hasNext())
		{
			ListItem list = li.next();
			this.addCompWeight(pnl, list.getWikiItem(), gbc, GridBagConstraints.BOTH, 0, y, 1, 1, 0.6, 0.0);
			this.addCompWeight(pnl, list.getJLabelAuthor(), gbc, GridBagConstraints.BOTH, 1, y, 1, 1, 0.3, 0.0);
			this.addCompWeight(pnl, list.getCheckBox(), gbc, GridBagConstraints.BOTH, 2, y++, 1, 1, 0.1, 0.0);
		}
		
		JPanel dummyPanel = new JPanel();
		this.addCompWeight(pnl, dummyPanel, gbc, GridBagConstraints.BOTH, 0, y, 1, 1, 1.0, 1.0);
		pnl.revalidate();
		pnl.repaint();
	}
	/****************************************************************************
	 * GRIDBAGLAYOUT RELATED METHODS
	 ****************************************************************************/
	/*
	 * GridBagConstraints Properties
	 */
	
	
	private void setUpGBC()
	{
		gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.insets = new Insets(2,2,2,2);
		gbc.ipadx = 0;
		gbc.ipady = 0;
		gbc.weightx = 0;
		gbc.weighty = 0;
	}
	
	/*
	 * GridBagLayout Methods
	 */
	//adding component to the layout based on constraints
	private void addComp(Container parentCom, Component childCom, GridBagConstraints g, int x, int y, int numOfCol, int numOfRow)
	{
		g.gridx = x;
		g.gridy = y;
		g.gridwidth = numOfCol;
		g.gridheight = numOfRow;
		parentCom.add(childCom, g);
	}
  
	//this method allows to specify the constraint's FILL
	private void addCompFill(Container parentCom, Component childCom, GridBagConstraints g, int gbcFill, int x, int y, int numOfCol, int numOfRow)
	{
		int original_fill = g.fill;
		g.fill = gbcFill; 
		this.addComp(parentCom, childCom, g, x, y, numOfCol, numOfRow);
	
		g.fill = original_fill;
	}

	//this method allows to specify the constraint's WEIGHT
	private void addCompWeight(Container parentCom, Component childCom, GridBagConstraints g, int gbcFill, int x, int y, int numOfCol, int numOfRow, double weightX, double weightY)
	{
		int original_fill = g.fill;
		g.fill = gbcFill; 
		double original_WeightX = g.weightx;
		double original_WeightY = g.weighty;
		g.weightx = weightX;
		g.weighty = weightY;
		this.addComp(parentCom, childCom, g, x, y, numOfCol, numOfRow);
	
		g.weightx = original_WeightX;
		g.weighty = original_WeightY;
		g.fill = original_fill;
	}
	
	//this method allows to specify the constraint's ANCHOR
	private void addCompAnchor(Container parentCom, Component childCom, GridBagConstraints g, int gbcAnchor, int x, int y, int numOfCol, int numOfRow)
	{
		int original_anchor = g.anchor;
		
		g.anchor = gbcAnchor;
		this.addComp(parentCom, childCom, g, x, y, numOfCol, numOfRow);
		
		g.anchor = original_anchor;
	}
	
	/****************************************************************************
	 * LOOK & FEEL RELATED METHODS
	 ****************************************************************************/
	/*
	 * Method to create the titled borders
	 */
	private void createTitledPanels(TitledBorder tb, String title, JPanel pnl, int justify, int position, Border border)
	{
		tb = BorderFactory.createTitledBorder(title);
		tb.setTitleJustification(justify);
		tb.setTitlePosition(position);
		tb.setBorder(border);
		pnl.setBorder(tb);
	}
	
	/****************************************************************************
	 * THE SETTERS AND GETTERS
	 ****************************************************************************/
	public int getUserID(){return userID;}
	public int getUserLevel(){return UserLevel;}
	public int getContentType(){return ContentType;}
	public boolean getIsLogin(){return isLogin;}
	public boolean getIsAdmin(){return isAdmin;}
	public String getUsername(){return username;}
	
	public void setUserID(int userID){this.userID = userID;}
	public void setUserLevel(int level){this.UserLevel = level;}
	public void setContentType(int type){this.ContentType = type;}
	public void setIsLogin(boolean bool){isLogin = bool;}
	public void setIsAdmin(boolean bool){isAdmin = bool;}
	public void setUsername(String username){this.username = username;}
	
	
	/****************************************************************************
	 * PROGRAM ENTRY POINT
	 ****************************************************************************/
	public static void main(String[] args) throws IOException
	{
		new GUI();
	}
	
	/****************************************************************************
	* ACTION LISTENER SECTION:
	* All the action listeners
	****************************************************************************/
	public void addRegisterListener(ActionListener lsnRegister)
	{
		jbtRegister.addActionListener(lsnRegister);
	}
	/*@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getActionCommand().equals("Register"))
		{
			//Pop up the Register Frame
			System.out.println("Register");
		}
		else if(e.getActionCommand().equals("LogIn"))
		{
			//Pop up the Login Frame
			System.out.println("LogIn");
		}
		else if(e.getActionCommand().equals("LogOut"))
		{
			//Set the UserLevel back to GUEST and refresh the whole frame
			System.out.println("LogOut");
		}
		else if(e.getActionCommand().equals("Search"))
		{
			//Refresh the pnlContent's Wiki as the search results
			System.out.println("Search");
		}
		else if(e.getActionCommand().equals("AddWiki"))
		{
			//Refresh the pnlContent as Add Wiki
			System.out.println("AddWiki");
		}
		else if(e.getActionCommand().equals("ApproveWiki"))
		{
			//Refresh the pnlContent as a list of Awaiting approve wikis
			System.out.println("ApproveWiki");
		}
		else if(e.getActionCommand().equals("ReportedWiki"))
		{
			//Refresh the pnlContent as a list of reported wikis
			System.out.println("ReportedWiki");
		}
		else if(e.getActionCommand().equals("UpdateWelcome"))
		{
			//Popo up a input dialog box for new update welcome msg
			System.out.println("UpdateWelcome");
		}
		else if(e.getActionCommand().equals("SendChat"))
		{
			//Send chat
			System.out.println("SendChat");
		}
		else if(e.getActionCommand().equals("Edit"))
		{
			//Refresh the interface to become editable
			System.out.println("Edit");
		}
		else if(e.getActionCommand().equals("ReportAbuse"))
		{
			//Pop up to ask user to confirm reporting abuse
			//In back end there set the report abuse boolean to true
			//and wait admin to confirm abusive content
			System.out.println("ReportAbuse");
		}
		else if(e.getActionCommand().equals("Delete"))
		{
			//Pop up to ask user confirm deletion
			System.out.println("Delete");
		}
		else
		{
			//Prompt invalid command
		}
		
		
	}*/
	
	/****************************************************************************
	* METHODS FOR DEVELOPERS USED ONLY
	****************************************************************************/
	private ArrayList createArrayList()
	{
		int index = 0, author = 123;
		String aut = author+"";
		ArrayList<ArrayList> arr = new ArrayList<ArrayList>();
		ArrayList<String> arrString;
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Tchaikovsky likes guy");
		arrString.add("He just so gay");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Mozart doesn't compose with his fingers");
		arrString.add("So hard to play his pieces!");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Haydn is Beethoven's teacher");
		arrString.add("");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Haydn steals Beethoven's work");
		arrString.add("So Beethoven is very angry");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Chopin is from polland");
		arrString.add("He just so gay");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Tchaikovsky likes guy");
		arrString.add("And he went to France");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Beethoven has a curse on composers who wrote Symphonies");
		arrString.add("The composers died when composing the 8th...");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Beethoven is a genius");
		arrString.add("He wrote 9 Symphonies");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Schumann's already 200 years old");
		arrString.add("So Clara is 190 years old");
		arrString.add(aut);
		arr.add(arrString);
		
		arrString= new ArrayList<String>();			
		arrString.add(index+++"");
		arrString.add("Schumann's birthday is next Tuesday");
		arrString.add("bila bala");
		arrString.add(aut);
		arr.add(arrString);
		
		return arr;
	}
}

/*********************************************************/
